/*
 * Copyright © Jack魏 2022 - 2022, All Rights Reserved.
 */

package com.jack.thread.t02;

import org.junit.Test;

/**
 * start和run的方法区别
 *
 * @author Jack魏
 * @since 2022/11/19 21:31
 */
public class StartRunThread {
    /**
     * 1. 测试启动两次
     */
    @Test
    public void test1() {
        MyThread myThread = new MyThread();
        myThread.start();
        // 第二次调用会抛出异常：IllegalThreadStateException
        myThread.start();
    }

    /**
     * 2. 测试run方法
     * 可以看到，run调用就是普通的方法调用，并没有启动线程去执行
     */
    @Test
    public void test2() {
        MyThread myThread = new MyThread();
        myThread.run();
        myThread.run();
    }
}

/**
 * 线程类
 */
class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println(Thread.currentThread().getName() + " while running...");
    }
}